package com.orion.controller;

import com.orion.entity.InnerTrans;
import com.orion.entity.Trans;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2021/10/19
 */

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/getWithInt")
    public Integer getWithInt(@RequestParam Integer i) {
        return i + 10;
    }

    @GetMapping("/getWithDouble")
    public Double getWithDouble(@RequestParam Double d) {
        return d + 10;
    }

    @GetMapping("/getWithString")
    public String getWithString(@RequestParam String str) {
        return "provider-b test getWithString : " + str + " return";
    }

    @GetMapping("/getWithPvInt/{id}")
    public String getWithPvInt(@PathVariable(name = "id") Integer id) {
        return "provider-b test getWithPvInt : " + id + " return";
    }

    @GetMapping("/getWithPvStr/{code}")
    public String getWithPvStr(@PathVariable(name = "code") String code) {
        return "provider-b test getWithPvStr : " + code + " return";
    }

    @GetMapping("/getWithMap")
    public Map getWithMap(@RequestParam Map m) {
        System.out.println(m);
        return m;
    }

    @GetMapping("/getWithBean")
    public Trans getWithBean(Trans t) {
        System.out.println(t.toString());
        t.setName("provider-b test getBean return");
        return t;
    }

    @GetMapping("/getWithSimpleBean")
    public InnerTrans getWithSimpleBean(InnerTrans t) {
        System.out.println(t.toString());
        t.setCode("provider-b test getBean return");
        return t;
    }

    @GetMapping("/getString")
    public String getString() {
        return "provider-b test getString";
    }

    @GetMapping("/getBean")
    public Trans getBean() {
        Trans trans = new Trans();
        trans.setDate(new Date());
        trans.setLdt(LocalDateTime.now().minusYears(1L));
        trans.setId(99);
        trans.setMoney(99.99);
        trans.setName("provider-b test getBean");

        InnerTrans innerTrans = new InnerTrans();
        innerTrans.setAge(88);
        innerTrans.setCode("inner");
        innerTrans.setFavor(Arrays.asList("fefe","ggg"));
        innerTrans.setTime(LocalDateTime.now());
        innerTrans.setDate(new Date());

        trans.setInnerTrans(innerTrans);
        return trans;
    }


    //----------------  post -----------------

    @PostMapping("/postWithInt")
    public Integer postWithInt(@RequestParam Integer i){
        return i + 20;
    }

    @PostMapping("/postWithDouble")
    public Double postWithDouble(@RequestParam Double d){
        return d + 4;
    }

    @PostMapping("/postWithString")
    public String postWithString(@RequestParam String s){
        return "test postWithString : provider-b receive " + s + " and return";
    }

    @PostMapping("/postWithMap")
    public Map postWithMap(@RequestBody Map m){
        System.out.println(m);
        return m;
    }

    @PostMapping("/postWithList")
    public List<Trans> postWithList(@RequestBody List<Trans> l){
        System.out.println(Arrays.toString(l.toArray()));
        return l;
    }

    @PostMapping("/postWithBean")
    public Trans postWithBean(@RequestBody Trans t){
        System.out.println(t.toString());
        return t;
    }

    @PostMapping("/postWithSimpleBean")
    public InnerTrans postWithSimpleBean(@RequestBody InnerTrans t){
        System.out.println(t.toString());
        return t;
    }

    //------------------ put ------------------

    @PutMapping("/putBean")
    public Trans putBean(@RequestBody Trans trans) {
        System.out.println(trans.toString());
        trans.setName("provider-b test putBean return");
        return trans;
    }

    @DeleteMapping("/deleteInter")
    public String deleteInter(Integer i) {
        return "provider-a test deleteInter get :" + i + " return";
    }

}
